package com.hdj.二叉树;

/**
 * @Author: HeDongJia
 * @Description:
 * @Date: Create in 23:19 2021/8/23
 * @Modified By:
 */

public class BinaryTreeTest {
    public static void main(String[] args) {
        //创建一个二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建结点
        GirlNode root = new GirlNode(1, "比比东");
        GirlNode node2 = new GirlNode(2, "云韵");
        GirlNode node3 = new GirlNode(3, "美杜莎");
        GirlNode node4 = new GirlNode(4, "纳兰嫣然");
        GirlNode node5 = new GirlNode(5, "雅妃");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node4);
        node3.setRight(node5);
        binaryTree.setRoot(root);
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.midOrder();
        System.out.println("后序遍历");
        binaryTree.postOrder();
        binaryTree.delNode(3);
        System.out.println("删除结点3，前序遍历");
        binaryTree.preOrder();
    }
}

//定义BinaryTree 二叉树
class BinaryTree {
    private GirlNode root;

    public GirlNode getRoot() {
        return root;
    }

    public void setRoot(GirlNode root) {
        this.root = root;
    }
    //前序遍历
    public void preOrder() {
        if(this.root != null) {
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空，不能遍历");
        }
    }
    //中序遍历
    public void midOrder() {
        if(this.root != null) {
            this.root.midOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
    //后序遍历
    public void postOrder() {
        if(this.root != null) {
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    //删除结点
    public void delNode(int no) {
        if(root != null) {
            //如果只有一个root结点, 这里立即判断root是不是就是要删除结点
            if(root.getNo() == no) {
                root = null;
            } else {
                //递归删除
                root.delNode(no);
            }
        }else{
            System.out.println("空树，不能删除~");
        }
    }
}
